
document.write( '<link rel="stylesheet" type="text/css" href="' + host + '/content/js/uploadify/uploadify.css?v=' + new Date().getMilliseconds() + '" media="all" />' );
document.write( '<script src="' + host + '/content/js/uploadify/uploadify.js?v=' + new Date().getMilliseconds() + '" charset="utf-8"></script>' );


$( ".item" ).live( 'mouseover', function () {
    $( this ).css( 'background', '#ffa' );
});

$( ".item" ).live( 'mouseout', function () {
    $( this ).css( 'background', '#fff' );
});

$( ".item .name" ).live( 'mouseover', function ( e ) {
    var isImg = $( this ).parent().find( '.img' ).html();
    if ( isImg == 1 ) {
        var xOffset = 180;
        var yOffset = 80;
        $( "#itemImg" ).remove();
        var file = $( this ).parent().find( '.file' ).html();
        if( file ){
            file = getFilePath( file );
            $( "<img id='itemImg' onerror='errorImg(this)' src='" + file + "' />" ).appendTo( "body" );
            $( "#itemImg" ).css( "top", (e.pageY - xOffset) + "px" ).css( "left", (e.pageX + yOffset) + "px" ).fadeIn( 1000 );
        }
    }
});

$( ".item .name" ).live( 'mouseout', function () {
    $( "#itemImg" ).remove();
});



$(".uploadify-del" ).live("click", function(){
    var obj = $( this ).attr( 'value' ) ;
    var attid = $(this ).parent().parent().attr('id');
    var url = del_url +  $( "#" + attid ).find( ".id" ).html();
    $( obj ).uploadify( 'cancel', attid );
    $.get( url, function ( result ) {
        if ( result == 'success' ) {

            // 删除表单数据
            var attach = $( 'input[name="attach[id]"]' );
            var file = $.trim( $( "#" + attid ).find( ".id" ).html() );
            if ( file.length > 0 ) {
                file = file + ",";
            }
            var newfile = attach.val().replace( file, "" );
            attach.val( newfile );

            // 删除编辑器里面数据
            if( typeof (editor1) != "undefined" ){
                var html = editor1.html();
                var img = $( "#" + attid ).find( ".img" ).html();
                var f = img == 0 ? $( "#" + attid ).find( ".id" ).html() : $( "#" + attid ).find( ".file" ).html();
                var src = getHTML( $( "#" + attid ).find( ".img" ).html(), f );
                var newHTML = html.replace( src, "" );
                editor1.html( newHTML );

                if( $( ".item" ).size() <= 1 ){
                    $( obj + "-queue" ).fadeOut( 4000, function () {
                        $( obj + "-queue" ).remove();
                    });
                }
            }
        }
    });
});
$(".uploadify-default" ).live("click", function(){
    var obj = $( this );
    var id = obj.parent().parent().find( ".id" ).html();
    var v = obj.attr( 'value' );
    var img = obj.parent().parent().find( ".img" ).html();
    if ( img == 0 ){
        jBox.tip( "只有图片才能设置缩略图!" ); return;
    }
    var url = default_url + id + "&v=" + v;
    $.get( url, function ( result ) {
        if( result == 'success'){
            if( v == 0 ){
                jBox.tip( "设置缩略图完成!" );
                obj.attr( 'value', '1' );
                obj.attr( 'title', '取消' );
            }else{
                obj.attr( 'value', '0' );
                obj.attr( 'title', '设置为缩略图片' );
                jBox.tip( "取消缩略图完成!" );
            }
        }
    });
});
$(".uploadify-insert" ).live("click", function(){
    var obj = $( this ).parent().parent();
    var img = obj.find( '.img' ).html();
    var file = img == 1 ? obj.find( '.file' ).html() : obj.find( '.id' ).html();
    addAttach( img, file );
});

function delAttach( obj, url, a, b ) {
    if ( typeof (editor1) != "undefined" && editor1.designMode == false ) {
        alert( '请先将编辑器切换到可视化模式!' );
        return false;
    }

    $.get( url, function ( result ) {
        if ( result == 'success' ) {
            if( typeof (editor1) != "undefined" ){
                var html = editor1.html();
                var src = getHTML( a, b );
                var newHTML = html.replace( src, "" );
                editor1.html( newHTML );
            }
            obj.fadeOut( 1000, function () {
                obj.remove();
                if ( $( ".attach" ).size() < 1 ) {
                    $( ".attachs" ).parent().parent().fadeOut( 1000 );
                }
            });
        }
    });
}

function addAttach( a, b ){
    var html = getHTML( a, b );
    insert( html );
}

function getHTML( a, b ) {
    var html = "";
    if ( a == 0 ) {
        html = '[attach=' + b + ']';
    }
    if ( a == 1 ) {
        html = '<img src="' + getFilePath( b ) + '" />';
    }
    return html;
}